//
//  AppDelegate.swift
//  KXBook
//
//  Created by  on 2022/9/7.
//

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window : UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        self.defaulType(.pay)
        self.defaulType(.In)
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = KXTabBarViewController()
        self.window?.makeKeyAndVisible()
        return true
    }
    
    
    lazy var user:User = {
       return User.bg_findAll(nil)?.first as? User ?? User()
    }()
    
    func defaulType(_ pay:KXAddType){
        let whereStr = String(format:"where %@=%@",bg_sqlKey("isPay"),bg_sqlValue("\(pay == .pay ? 1 : 0)"))
        let count = KXTypeModel.bg_count(nil, where: whereStr)
        if count <= 0{
            let arr1 = ["1","2","3"]
            let arr2 = ["交通","餐饮","娱乐"]
            for i in 0..<arr1.count{
                let model = KXTypeModel()
                model.isPay = pay == .pay ? 1 : 0
                model.type = Int32(arr1[i].intValue())
                model.name = arr2[i]
                model.bg_saveOrUpdate()
            }
        }
    }
}

